﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using TSV.Models;
using TSV.Areas.Administration.Models;
using System.Collections;
using TSV.Library.Common;

namespace TSV.Library.GenericHandle
{
    /// <summary>
    /// Summary description for DropdownListHandler
    /// </summary>
    public class DropdownListHandler : IHttpHandler
    {
        #region private variablees

        private DistrictDao distDao = new DistrictDao();
        
        #endregion
        /// <summary>
        /// Get Json String View Multi Column in Dropdownlist
        /// </summary>
        /// <param name="list">List of data </param>
        /// <param name="columns">Array column data</param>
        /// <returns></returns>
        private StringBuilder GetStringBuilder(IEnumerable list, string[] columns)
        {
            
               
            StringBuilder strBuilder = new StringBuilder();
            if (((IList)list).Count==0)
                return strBuilder;
            strBuilder.Append("[");
            foreach (Object item in list)
            {
                strBuilder.Append("{");
                foreach (string column in columns)
                {
                    strBuilder.Append("\"" + column + "\":\"" + item.GetType().GetProperty(column).GetValue(item, null) + "\",");
                }
                strBuilder.Remove(strBuilder.Length - 1, 1);
                strBuilder.Append("},");
               
            }
            strBuilder.Remove(strBuilder.Length - 1, 1);
            strBuilder.Append("]");
            return strBuilder;
        }
        /// <summary>
        /// Get Json String View Column name in Dropdownlist
        /// </summary>
        /// <param name="list">List of data</param>
        /// <param name="DataTextFiled">Column name view Text</param>
        /// <param name="DataValueField">Column name Value</param>
        /// <returns></returns>
        private StringBuilder GetStringBuilder(IEnumerable list,string DataTextFiled,string DataValueField)
        {


            StringBuilder strBuilder = new StringBuilder();
            if (((IList)list).Count == 0)
                return strBuilder;
            strBuilder.Append("[");
            foreach (Object item in list)
            {
                strBuilder.Append("{");
                strBuilder.Append("\"Name\":\"" + item.GetType().GetProperty(DataTextFiled).GetValue(item, null) + "\",");
                strBuilder.Append("\"ID\":\"" + item.GetType().GetProperty(DataValueField).GetValue(item, null) + "\"");
                strBuilder.Append("},");

            }
            strBuilder.Remove(strBuilder.Length - 1, 1);
            strBuilder.Append("]");
            return strBuilder;
        }
        public void ProcessRequest(HttpContext context)
        {

            string ID = context.Request.QueryString["ID"];
            string pageName = context.Request.QueryString["Page"];
            StringBuilder strBuilder = new StringBuilder();
            switch (pageName)
            {
                case "District":
                    List<District> list = distDao.GetByProvinceID(int.Parse(ID));
                    strBuilder = GetStringBuilder(list, "DistrictName", "ID");
                    break;
                case "Province":
                    List<District> distList = distDao.GetByProvinceID(int.Parse(ID));
                    strBuilder = GetStringBuilder(distList, "DistrictName", "ID");
                    break;
            }
            context.Response.ContentType = "application/json";
            context.Response.Write(strBuilder.ToString());
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}